
function toDateStr(date) {
  const arr = date.toLocaleDateString().split('/') // 2021/4/1
  if (arr[1].length < 2) {
    arr[1] = '0' + arr[1]
  }
  if (arr[2].length < 2) {
    arr[2] = '0' + arr[2]
  }
  return arr.join('-') // 2021-04-01
}

function toDateTimeStr(date) {
  const dateStr = toDateStr(date)
  let timeStr = ''
  timeStr += date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  timeStr += ':'
  timeStr += date.getMinutes() < 10 ? '0' + date.getMinutes()() : date.getMinutes()
  timeStr += ':'
  timeStr += date.getSeconds() < 10 ? '0' + date.getSeconds()() : date.getSeconds()
  return `${dateStr} ${timeStr}`
}

function parse(dateStr) {
  const arr = dateStr.toLocaleDateString().split('-') // 2021-04-01
  if (arr[1].length > 1) {
    arr[1] = arr[1].charAt(1)
  }
  if (arr[2].length > 1) {
    arr[2] = arr[2].charAt(1)
  }
  return new Date(arr.join('/')) // 2021/4/1
}

export default {
  toDateStr,
  parse,
  toDateTimeStr
}
